package com.zg.message.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.R;
import com.zg.common.core.dao.message.entity.MsgInfo;
import com.zg.message.api.domain.req.MsgInfoAddReq;
import com.zg.message.domain.convert.MsgInfoConvert;

import com.zg.message.domain.req.MsgInfoEditReq;
import com.zg.message.domain.req.MsgInfoPageReq;
import com.zg.message.domain.req.MsgInfoVerifyReq;
import com.zg.message.domain.vo.MsgInfoVo;
import com.zg.message.service.MsgInfoService;
import com.zg.message.sys.SendStateEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/5/22 10:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/message/info")
@Slf4j
@Api(tags = "消息管理")
public class MsgInfoController {
    private final MsgInfoService msgInfoService;

    private final MsgInfoConvert     msgInfoConvert;

    @PostMapping("/page")
    public R<Page<MsgInfoVo>> page(@RequestBody MsgInfoPageReq pageReq) {
        Page<MsgInfoVo> page = msgInfoService.page(pageReq,MsgInfoVo.class);
        return R.success(page);
    }


    @PostMapping("/add")
    @ApiOperation("发送短信")
    public R<Long> add(@RequestBody @Valid MsgInfoAddReq addReq) {
        if(addReq.getSendTime()==null){
            addReq.setSendTime(new Date());
        }
        addReq.setSendState(SendStateEnum.INIT.getValue());
        Long id=msgInfoService.add(addReq);
        return R.success(id);
    }

    @PostMapping("/edit")
    public R edit(@RequestBody  @Valid MsgInfoEditReq editReq) {
        msgInfoService.edit(editReq);
        return R.success();
    }

    @PostMapping("/remove")
    public R remove(@RequestBody  List<Long> ids) {
        msgInfoService.remove(ids);
        return R.success();
    }

    @GetMapping("/listByReceiver")
    public R<List<MsgInfoVo>> listByReceiver(@RequestParam Long userId) {
        List<MsgInfo> list =msgInfoService.listByReceiver(userId);
        return R.success(msgInfoConvert.toVoList(list));
    }

    @PostMapping("/verify")
    @ApiOperation("验证短信")
    public R<Boolean> verify(@RequestBody @Valid MsgInfoVerifyReq req) {
        Boolean msgInfo =msgInfoService.verifySmsCode(req);
        return R.success(msgInfo);
    }

}
